package com.xinhe.web.service;

import org.codehaus.jettison.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.IOException;
@Service
public class AdapayAddressService {
  /*  @Autowired
    private IAdapayAddressDao adapayAddressDao;

    public PageResult<AdapayAddressView> pageAdapayAddress(AdapayAddressQuery query) {
        Integer total = Integer.valueOf(0);
        if (0 < query.getLimit().intValue()) {
            total = this.adapayAddressDao.countAdapayAddress(query);
        }
        List<AdapayAddressView> views = null;
        if (0 < total.intValue() || 0 >= query.getLimit().intValue()) {
            views = this.adapayAddressDao.pageAdapayAddress(query);
        }
        if (0 >= query.getLimit().intValue()) {
            if (null == views) {
                total = Integer.valueOf(0);
            } else {
                total = Integer.valueOf(views.size());
            }
        }
        return new PageResult(total, views);
    }



    @Transactional
    public BaseResult readAdapayAddress(String filePath) {
        try {
            String jsonStr = FileUtils.readFileToString(new File(filePath), "UTF-8");
            JSONObject jsonObject = JSONObject.parseObject(jsonStr);

            for (String province : jsonObject.keySet()) {
                JSONObject provinceObj = jsonObject.getJSONObject(province);

                String provinceVal = provinceObj.getString("val");

                AdapayAddress adapayAddress = new AdapayAddress();
                adapayAddress.setType(Integer.valueOf(2));
                adapayAddress.setLevel(Integer.valueOf(1));
                adapayAddress.setAddrCode(provinceVal);
                adapayAddress.setAddrName(province);
                this.adapayAddressDao.save((BaseEntityImpl)adapayAddress);

                JSONObject cityJsonList = provinceObj.getJSONObject("items");
                for (String city : cityJsonList.keySet()) {
                    JSONObject cityObj = cityJsonList.getJSONObject(city);

                    String cityeVal = cityObj.getString("val");

                    AdapayAddress cityAddress = new AdapayAddress();
                    cityAddress.setParentId(adapayAddress.getId());
                    cityAddress.setType(Integer.valueOf(2));
                    cityAddress.setLevel(Integer.valueOf(2));
                    cityAddress.setAddrCode(cityeVal);
                    cityAddress.setAddrName(city);
                    this.adapayAddressDao.save((BaseEntityImpl)cityAddress);

                    JSONObject areaJsonList = cityObj.getJSONObject("items");
                    if (areaJsonList != null) {
                        for (String area : areaJsonList.keySet()) {
                            String areaVal = areaJsonList.getString(area);

                            AdapayAddress areaAddress = new AdapayAddress();
                            areaAddress.setParentId(cityAddress.getId());
                            areaAddress.setType(Integer.valueOf(2));
                            areaAddress.setLevel(Integer.valueOf(3));
                            areaAddress.setAddrCode(areaVal);
                            areaAddress.setAddrName(area);
                            this.adapayAddressDao.save((BaseEntityImpl)areaAddress);
                        }

                    }
                }

            }

        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return new BaseResult();
    }



    @Transactional
    public BaseResult readAdapayAddressResident(String filePath) {
        try {
            String jsonStr = FileUtils.readFileToString(new File(filePath), "UTF-8");
            JSONObject jsonObject = JSONObject.parseObject(jsonStr);

            for (String province : jsonObject.keySet()) {
                JSONObject provinceObj = jsonObject.getJSONObject(province);

                String provinceVal = provinceObj.getString("val");

                AdapayAddress adapayAddress = new AdapayAddress();
                adapayAddress.setType(Integer.valueOf(2));
                adapayAddress.setLevel(Integer.valueOf(1));
                adapayAddress.setAddrCode(provinceVal);
                adapayAddress.setAddrName(province);
                this.adapayAddressDao.save((BaseEntityImpl)adapayAddress);

                JSONObject cityJsonList = provinceObj.getJSONObject("items");
                for (String city : cityJsonList.keySet()) {
                    JSONObject cityObj = cityJsonList.getJSONObject(city);

                    String cityeVal = cityObj.getString("val");

                    AdapayAddress cityAddress = new AdapayAddress();
                    cityAddress.setParentId(adapayAddress.getId());
                    cityAddress.setType(Integer.valueOf(2));
                    cityAddress.setLevel(Integer.valueOf(2));
                    cityAddress.setAddrCode(cityeVal);
                    cityAddress.setAddrName(city);
                    this.adapayAddressDao.save((BaseEntityImpl)cityAddress);

                    JSONObject areaJsonList = cityObj.getJSONObject("items");
                    if (areaJsonList != null) {
                        for (String area : areaJsonList.keySet()) {
                            String areaVal = areaJsonList.getString(area);

                            AdapayAddress areaAddress = new AdapayAddress();
                            areaAddress.setParentId(cityAddress.getId());
                            areaAddress.setType(Integer.valueOf(2));
                            areaAddress.setLevel(Integer.valueOf(3));
                            areaAddress.setAddrCode(areaVal);
                            areaAddress.setAddrName(area);
                            this.adapayAddressDao.save((BaseEntityImpl)areaAddress);

                        }

                    }
                }

            }

        }
        catch (IOException e) {
            e.printStackTrace();
        }
        return new BaseResult();
    }


    public ListResult<AdapayAddress> getAdapayAddress(Integer type, Integer level, Long parentId) {
        List<AdapayAddress> adapayAddressList = this.adapayAddressDao.getAdapayAddress(type, level, parentId);
        return new ListResult(adapayAddressList);
    }


    protected IBaseDao<AdapayAddress> getRealDao() {
        return (IBaseDao<AdapayAddress>)this.adapayAddressDao;
    }*/
}
